<%@ page import="java.util.*" import="com.hospital.util.DbUtil" import="com.hospital.util.Pagination"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page language="java" import="java.sql.*"%>
<%@ page language="java" import="com.hospital.model.UserInfo"%>
<%@ page language="java" import="java.text.SimpleDateFormat"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link href="images/main.css" rel="stylesheet" type="text/css" />
<link  href="../css/style.css" rel="stylesheet" type="text/css" />
<title></title>
</head>
<body>
	<jsp:include page="right_top.jsp"></jsp:include>
	
	<!-- TOP START -->
	<div id="right_font">
		<img src="images/main_14.gif" /> 您现在所在的位置：首页 → 就诊信息管理 → <span
			class="bfont">填写就诊信息</span>
	</div>
	<!-- TOP END -->
	
	<!-- MAIN START -->
	<%  
    	UserInfo user = (UserInfo)(session.getAttribute("userInfo")); 
   		String root = null; 
   		String deptId = null;
   		String doctorName = null;
   		if(user != null){ 
   			deptId = user.getDeptId();
   			doctorName = user.getAccount();
  			root = user.getRoot(); 
    	} 
    	if("1".equals(root)){ 
    %>
	<form name="form" id="form">
		<table width="100%" border="0" cellspacing="0" cellpadding="0">
			<tr>
				<td height="30">
					&nbsp;
				</td>
			</tr>
			<tr>
				<td>
					<!-- table context start -->
					<table width="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="#c9c9c9">
						<tr>
							<td height="22" bgcolor="#FFFFFF">
								<div align="center">
									<strong><span class="STYLE1">编号</span></strong>
								</div>
							</td>
							<td height="22" bgcolor="#FFFFFF">
								<div align="center">
									<strong><span class="STYLE1">挂号单号</span></strong>
								</div>
							</td>
							<td height="22" bgcolor="#FFFFFF">
								<div align="center">
									<strong><span class="STYLE1">姓名</span></strong>
								</div>
							</td>
							<td height="22" bgcolor="#FFFFFF">
								<div align="center">
									<strong><span class="STYLE1">年龄</span></strong>
								</div>
							</td>
							<td height="22" bgcolor="#FFFFFF">
								<div align="center">
									<strong><span class="STYLE1">科室</span></strong>
								</div>
							</td>
							<td height="22" bgcolor="#FFFFFF">
								<div align="center">
									<strong><span class="STYLE1">挂号类型</span></strong>
								</div>
							</td>
							<td height="22" bgcolor="#FFFFFF">
								<div align="center">
									<strong><span class="STYLE1">操作</span></strong>
								</div>
							</td>
						</tr>
						<%
							if(request.getParameter("orderid")!=null){
								String orderid = request.getParameter("orderid");
								String sql_finish = "update t_order set finish = '"+ "yes" +"', finishtime = '"+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime()) +"' where oid = " + orderid;
								DbUtil.add_modify(sql_finish);
							}
						%>
						<%
							/**
							 * 分页begin
							 */
							final int pageSize = 10; // 一页显示 10 条记录
		
							int pageNum = 1; // 当前页数
							int pageCount = 1; // 总页数
		
							int recordCount = 0; // 总记录数
		
							String strAbsPath = application.getRealPath(request.getRequestURI()); // 获得当前文件的绝对路径名
							int endIndex = strAbsPath.length();
							int beginIndex = strAbsPath.lastIndexOf("\\") + 1;
							String pageUrl = strAbsPath.substring(beginIndex, endIndex); // 获取当前文件名，如：xxx.jsp,或者 xxx.html
		
							try {
								// 从地址栏参数取当前页数
								if (request.getParameter("pageNum") == null) {
									pageNum = 1;
								} else {
									pageNum = Integer.parseInt(request.getParameter("pageNum"));
								}
							} catch (Exception e) {
								e.printStackTrace();
							}
		
							recordCount = DbUtil.getCount("t_order");
							// 计算总页数
							pageCount = (recordCount + pageSize - 1) / pageSize;
							// 本页从 startRecord 行开始
							int startRecord = (pageNum - 1) * pageSize;
							/**
							 * 分页end
							 */
							Connection conn = null;
							PreparedStatement stmt = null;
							ResultSet rs = null;
							String deptName = null;
							int num = 1;
							String sql = "select * from t_order order by id desc limit " + startRecord + " ," + pageSize;
							List<Map<String, Object>> list = DbUtil.find(sql);
							Map<String, Object> map = null;
							for (int i = 0; i < list.size(); i++) {
								map = (Map<String, Object>)list.get(i);
								String sql1 = "select name from t_department where deptId = ?";
								conn = DbUtil.getConnection();
								try {
									stmt = conn.prepareStatement(sql1);
									stmt.setString(1, (String)map.get("guahaoDeptId"));
									rs = stmt.executeQuery();
									while(rs.next()){
										deptName = rs.getString(1);
									}
								}catch (SQLException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
						%>
						<tr>
							<td height="22" bgcolor="#FFFFFF">
								<div align="center">
									<span class="STYLE3"><%=num++%></span>
								</div>
							</td>
							<td height="22" bgcolor="#FFFFFF">
								<div align="center">
									<span class="STYLE3"><%=map.get("oid")%></span>
								</div>
							</td>
							<td height="22" bgcolor="#FFFFFF">
								<div align="center">
									<span class="STYLE3"><%=map.get("name")%></span>
								</div>
							</td>
							<td height="22" bgcolor="#FFFFFF">
								<div align="center">
									<span class="STYLE3"><%=map.get("age")%></span>
								</div>
							</td>
							<td height="22" bgcolor="#FFFFFF">
								<div align="center">
									<span class="STYLE3"><%=deptName%></span>
								</div>
							</td>
							<td height="22" bgcolor="#FFFFFF">
								<div align="center">
									<span class="STYLE3"><%=map.get("type")%></span>
								</div>
							</td>
							<td height="22" bgcolor="#FFFFFF">
								<div align="center">
								<%if(map.get("finish").equals("no")){%>
									<span class="STYLE3"><a href="cure_add2.jsp?orderid=<%=map.get("oid")%>">就诊</a>
									<a href="cure_add.jsp?orderid=<%=map.get("oid")%>">结束就诊</a></span>
								<%}else{%>
									<span class="STYLE3"><font color="#3A6279"><%=map.get("finishtime")%>结束就诊</font></span>
								<%} %>
								</div>
							</td>
							
						</tr>
						<%
							}
						%>
					</table>
				<!-- table context end -->
				</td>
			</tr>
			<tr>
				<td height="35">
					<!-- page start -->
					<table width="100%" border="0" cellspacing="0" cellpadding="1">
						<tr>
							<td width="75%" valign="top" class="STYLE1">
								<div align="right">
									<%=Pagination.getPagination(pageNum, pageCount, recordCount,pageUrl)%>&nbsp;&nbsp;
								</div>
							</td>
						</tr>
					</table>
					<!-- page end -->
				</td>
			</tr>
		</table>
	</form>
	<%
		}else if("2".equals(root)){ 
	%>
	<form name="form1" id="form1">
		<table width="100%" border="0" cellspacing="0" cellpadding="0">
			<tr>
				<td height="30">
					&nbsp;
				</td>
			</tr>
			<tr>
				<td>
					<!-- table context start -->
					<table width="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="#c9c9c9">
						<tr>
							<td height="22" bgcolor="#FFFFFF">
								<div align="center">
									<strong><span class="STYLE1">编号</span></strong>
								</div>
							</td>
							<td height="22" bgcolor="#FFFFFF">
								<div align="center">
									<strong><span class="STYLE1">挂号单号</span></strong>
								</div>
							</td>
							<td height="22" bgcolor="#FFFFFF">
								<div align="center">
									<strong><span class="STYLE1">姓名</span></strong>
								</div>
							</td>
							<td height="22" bgcolor="#FFFFFF">
								<div align="center">
									<strong><span class="STYLE1">年龄</span></strong>
								</div>
							</td>
							<td height="22" bgcolor="#FFFFFF">
								<div align="center">
									<strong><span class="STYLE1">科室</span></strong>
								</div>
							</td>
							<td height="22" bgcolor="#FFFFFF">
								<div align="center">
									<strong><span class="STYLE1">挂号类型</span></strong>
								</div>
							</td>
							<td height="22" bgcolor="#FFFFFF">
								<div align="center">
									<strong><span class="STYLE1">就诊操作/结束就诊</span></strong>
								</div>
							</td>
						</tr>
						<%
							if(request.getParameter("orderid")!=null){
								String orderid = request.getParameter("orderid");
								String sql_finish = "update t_order set finish = '"+ "yes" +"', finishtime = '"+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime()) +"' where oid = " + orderid;
								DbUtil.add_modify(sql_finish);
							}
						%>
						<%
							/**
							 * 分页begin
							 */
							final int pageSize = 10; // 一页显示 10 条记录
		
							int pageNum = 1; // 当前页数
							int pageCount = 1; // 总页数
		
							int recordCount = 0; // 总记录数
		
							String strAbsPath = application.getRealPath(request.getRequestURI()); // 获得当前文件的绝对路径名
							int endIndex = strAbsPath.length();
							int beginIndex = strAbsPath.lastIndexOf("\\") + 1;
							String pageUrl = strAbsPath.substring(beginIndex, endIndex); // 获取当前文件名，如：xxx.jsp,或者 xxx.html
		
							try {
								// 从地址栏参数取当前页数
								if (request.getParameter("pageNum") == null) {
									pageNum = 1;
								} else {
									pageNum = Integer.parseInt(request.getParameter("pageNum"));
								}
							} catch (Exception e) {
								e.printStackTrace();
							}
		
							Connection conn0 = null;
							PreparedStatement stmt0 = null;
							ResultSet rs0 = null;
							String sql0 = "select * from t_order where guahaoDeptId = ? and doctorName = ?";
							///////////////////////////////////////////////////////////////////////////////////////////////////////
							conn0 = DbUtil.getConnection();
							List<Map<String, Object>> list0 = new ArrayList<Map<String, Object>>();
							try {
								stmt0 = conn0.prepareStatement(sql0);
								stmt0.setString(1, deptId);
								stmt0.setString(2, doctorName);
								rs0 = stmt0.executeQuery();
								ResultSetMetaData md = rs0.getMetaData();
								int columnCount = md.getColumnCount();
								while (rs0.next()) {
									Map<String, Object> map1 = new HashMap<String, Object>();
									for (int i = 1; i <= columnCount; i++) {
										map1.put(md.getColumnName(i), rs0.getObject(i));
									}
									list0.add(map1);
								}
								recordCount = list0.size();
							} catch (SQLException e) {
								e.printStackTrace();
							}
							// 计算总页数
							pageCount = (recordCount + pageSize - 1) / pageSize;
							// 本页从 startRecord 行开始
							int startRecord = (pageNum - 1) * pageSize;
							/**
							 * 分页end
							 */
							Connection conn = null;
							PreparedStatement stmt = null;
							ResultSet rs = null;
							///////////////////////////////////////////////////////////////////////////////////////////////////////
							Connection conn1 = null;
							PreparedStatement stmt1 = null;
							ResultSet rs1 = null;
							String deptName = null;
							int num = 1;
							String sql = "select * from t_order where guahaoDeptId = ? and doctorName = ?";
							///////////////////////////////////////////////////////////////////////////////////////////////////////
							conn1 = DbUtil.getConnection();
							List<Map<String, Object>> list1 = new ArrayList<Map<String, Object>>();
							try {
								stmt1 = conn1.prepareStatement(sql);
								stmt1.setString(1, deptId);
								stmt1.setString(2, doctorName);
								rs1 = stmt1.executeQuery();
								ResultSetMetaData md = rs1.getMetaData();
								int columnCount = md.getColumnCount();
								while (rs1.next()) {
									Map<String, Object> map1 = new HashMap<String, Object>();
									for (int i = 1; i <= columnCount; i++) {
										map1.put(md.getColumnName(i), rs1.getObject(i));
									}
									list1.add(map1);
								}
							} catch (SQLException e) {
								e.printStackTrace();
							}
							//////////////////////////////////////////////////////////////////////////////////////////////////////
							Map<String, Object> map2 = null;
							for (int i = 0; i < list1.size(); i++) {
								map2 = (Map<String, Object>)list1.get(i);
								String sql1 = "select name from t_department where deptId = ?";
								conn = DbUtil.getConnection();
								try {
									stmt = conn.prepareStatement(sql1);
									stmt.setString(1, (String)map2.get("guahaoDeptId"));
									rs = stmt.executeQuery();
									while(rs.next()){
										deptName = rs.getString(1);
									}
								}catch (SQLException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
						%>
						<tr>
							<td height="22" bgcolor="#FFFFFF">
								<div align="center">
									<span class="STYLE3"><%=num++%></span>
								</div>
							</td>
							<td height="22" bgcolor="#FFFFFF">
								<div align="center">
									<span class="STYLE3"><%=map2.get("oid")%></span>
								</div>
							</td>
							<td height="22" bgcolor="#FFFFFF">
								<div align="center">
									<span class="STYLE3"><%=map2.get("name")%></span>
								</div>
							</td>
							<td height="22" bgcolor="#FFFFFF">
								<div align="center">
									<span class="STYLE3"><%=map2.get("age")%></span>
								</div>
							</td>
							<td height="22" bgcolor="#FFFFFF">
								<div align="center">
									<span class="STYLE3"><%=deptName%></span>
								</div>
							</td>
							<td height="22" bgcolor="#FFFFFF">
								<div align="center">
									<span class="STYLE3"><%=map2.get("type")%></span>
								</div>
							</td>
							<td height="22" bgcolor="#FFFFFF">
								<div align="center">
									<span class="STYLE3">
										<div align="center">
											<%if(map2.get("finish").equals("no")){%>
												<span class="STYLE3"><a href="cure_add2.jsp?orderid=<%=map2.get("oid")%>">就诊</a>
												<a href="cure_add.jsp?orderid=<%=map2.get("oid")%>">结束就诊</a></span>
											<%}else{%>
												<span class="STYLE3"><font color="#3A6279"><%=map2.get("finishtime")%>结束就诊</font></span>
											<%} %>
										</div>
									</span>
								</div>
							</td>
						</tr>
						<%
							}
						%>
					</table>
				<!-- table context end -->
				</td>
			</tr>
			<tr>
				<td height="35">
					<!-- page start -->
					<table width="100%" border="0" cellspacing="0" cellpadding="1">
						<tr>
							<td width="75%" valign="top" class="STYLE1">
								<div align="right">
									<%=Pagination.getPagination(pageNum, pageCount, recordCount,pageUrl)%>&nbsp;&nbsp;
								</div>
							</td>
						</tr>
					</table>
					<!-- page end -->
				</td>
			</tr>
		</table>
	</form>
	<%	} %>
</body>
</html>
